// ==UserScript==
// @name         快捷下载预览网站图片（download-picture-script）
// @namespace    https://gitee.com/DieHunter/download-picture-script
// @version      1.1.4
// @description  快捷下载网站图片（油猴脚本），用于下载图片标签或者背景图
// @author       DieHunter
// @match        http*://*/*
// @icon         
// @grant        none
// @license      AGPL License
// ==/UserScript==

const download = function (url, name) {
  const a = document.createElement("a");
  a.href = url;
  a.download = name;
  a.target = "_blank";
  a.style.display = "none";
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
};
(function () {
  let current = null;
  let imgList = [];
  document.addEventListener("mouseover", function (event) {
    const target = event.target;
    event.stopPropagation();

    if (target.tagName === "IMG") {
      imgList = [target];
    } else {
      imgList = target.querySelectorAll("img");
    }
    if (imgList.length <= 0) {
      return;
    }
    current = target;
    target.style.border = "1px solid red";
  });
  document.addEventListener("mouseout", function (event) {
    const target = event.target;
    if (current === target) {
      target.style.border = "none";
      current = null;
      imgList = [];
    }
  });
  document.addEventListener("click", function (event) {
    if (event.altKey && imgList?.length) {
      // 执行你的操作
      imgList.forEach((img, index) => {
        if (img.src) {
          download(img.src, `${index}`);
        }
      });
    }
  });
})();
